﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMHTX.Modules.DIRECTORY.TEAM.Components;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Data;

namespace LMHTX.Modules.DIRECTORY.TEAM.Controls
{
    public partial class TeamList : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            try
            {
                DataSet ds = new DataSet();
                if (Request.QueryString["typeid"] != null && Int32.Parse(Request.QueryString["typeid"].ToString())!=0)
                {
                    //Get data theo loai hinh
                    ds = Components.Team.SelectTeam_SelectBy_TypeID(this.ModuleID, Int32.Parse(Request.QueryString["typeid"].ToString()));
                }
                else if (Request.QueryString["careerid"] != null && Int32.Parse(Request.QueryString["careerid"].ToString()) != 0)
                {
                    //Get data theo nganh nghe
                    ds = Components.Team.SelectTeam_SelectBy_CareerID(this.ModuleID, Int32.Parse(Request.QueryString["careerid"].ToString()));
                }
                else if (Request.QueryString["areaid"] != null && Int32.Parse(Request.QueryString["areaid"].ToString()) != 0)
                {
                    //Get data theo dia ban
                    ds = Components.Team.SelectTeam_SelectBy_CareerID(this.ModuleID, Int32.Parse(Request.QueryString["areaid"].ToString()));
                }
                else
                {
                    ds = Components.Team.SelectAll_AreaName(this.ModuleID);
                }
                gvTeam.DataSource = ds;
                gvTeam.DataBind();
                ViewState["dsTeam"] = ds;

                ddlArea.DataSource = Components.Area.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlArea.DataBind();

                ddlCareer.DataSource = Components.Career.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlCareer.DataBind();

                ddlType.DataSource = Components.Type.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlType.DataBind();

                ddlTraining.DataSource = Components.Training.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlTraining.DataBind();
            }
            catch { }
        }

        protected void gvTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTeam.PageIndex = e.NewPageIndex;
            if ((DataSet)ViewState["dsTeam"] != null)
            {
                gvTeam.DataSource = (DataSet)ViewState["dsTeam"];
                gvTeam.DataBind();
            }
            else
                LoadData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string where = string.Empty;
            string value = txtSearchValue.Text;

            if (txtSearchValue.Text.Length > 0)
            {
                if (txtSearchValue.Text.IndexOf("%") < 0)
                {
                    where += string.Format(" AND T.Title LIKE '%{0}%' AND AreaID LIKE '{1}' AND CareerID LIKE '{2}' AND TypeID LIKE '{3}' AND TrainingID LIKE '{4}' ", value, ddlArea.SelectedValue, ddlCareer.SelectedValue, ddlType.SelectedValue, ddlTraining.SelectedValue);
                    gvTeam.DataSource = Components.Team.Search(this.ModuleID, where);
                    gvTeam.DataBind();
                }
                else
                {
                    this.ShowMessage("Thông báo", "Không chấp nhận các ký tự đặc biệt");
                }
            }
            else
            {
                where += string.Format(" AND AreaID LIKE '{0}' AND CareerID LIKE '{1}' AND TypeID LIKE '{2}' AND TrainingID LIKE '{3}' ", ddlArea.SelectedValue, ddlCareer.SelectedValue, ddlType.SelectedValue, ddlTraining.SelectedValue);
                gvTeam.DataSource = Components.Team.Search(this.ModuleID, where);
                gvTeam.DataBind();
            }
        }
    }
}